package algotithm.weekendrace.biweeklycontest72.test1;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tom
 * @date 2022-03-05 22:28
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {11,22,11,33,11,33};
        int key = 11;
        int frequent = solution.mostFrequent(nums, key);
        System.out.println(frequent);
    }


    public int mostFrequent(int[] nums, int key) {
        int res = 0;
        int n = nums.length;
        int max = 0;
        Map<Integer,Integer> map = new HashMap<>();
        int i = 0;
        for (; i < n; ) {
            if (nums[i] == key && i + 1 < n) {
                map.put(nums[i + 1], map.getOrDefault(nums[i + 1], 0) + 1);
                if (map.get(nums[i + 1]) > max) {
                    max = map.get(nums[i + 1]);
                    res = nums[i + 1];
                }
                i += 2;
            } else {
                i++;
            }
        }
        return res;

    }
}
